/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.lawyer.drive.client;

import com.google.gwt.core.client.EntryPoint;


import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class LawyerDrive implements EntryPoint {
	
	private VerticalPanel verticalPanel;
	private FormPanel formPanel;
	private VerticalPanel verticalPanel_1;
	private Button btnNewButton;
	private FileUpload fileUpload;
	private TextBox textBox;

	@Override
	public void onModuleLoad() {
		final RootPanel rootPanel = RootPanel.get();
		rootPanel.add(this.getVerticalPanel(), 0, 0);
	}

	private VerticalPanel getVerticalPanel() {
		if (this.verticalPanel == null) {
			this.verticalPanel = new VerticalPanel();
			this.verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			this.verticalPanel.setWidth("100%");
			this.verticalPanel.add(this.getFormPanel());
		}
		return this.verticalPanel;
	}

	private FormPanel getFormPanel() {
		if (this.formPanel == null) {
			this.formPanel = new FormPanel();
			this.formPanel.setWidth("40%");
			this.formPanel.setWidget(this.getVerticalPanel_1());
			this.formPanel.setMethod(FormPanel.METHOD_POST);
			this.formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
			this.formPanel.setAction("/FileUploaderServelet");
			this.formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {

				@Override
				public void onSubmitComplete(final SubmitCompleteEvent event) {
					Window.alert("form was Submited successfully");
				}
			});
		}
		return this.formPanel;
	}

	private VerticalPanel getVerticalPanel_1() {
		if (this.verticalPanel_1 == null) {
			this.verticalPanel_1 = new VerticalPanel();
			this.verticalPanel_1.setSize("100%", "100%");
			this.verticalPanel_1.setCellHorizontalAlignment(this.getBtnNewButton(), HasHorizontalAlignment.ALIGN_CENTER);
			this.verticalPanel_1.add(this.getTextBox());
			this.verticalPanel_1.add(this.getFileUpload());
			this.verticalPanel_1.add(this.getBtnNewButton());
		}
		return this.verticalPanel_1;
	}

	private Button getBtnNewButton() {
		if (this.btnNewButton == null) {
			this.btnNewButton = new Button("Upload");
			
			this.btnNewButton.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(final ClickEvent event) {
					LawyerDrive.this.formPanel.setAction("/DriverManager");
					LawyerDrive.this.formPanel.submit();
				}
			});
		}
		return this.btnNewButton;
	}

	private FileUpload getFileUpload() {
		if (this.fileUpload == null) {
			this.fileUpload = new FileUpload();
			this.fileUpload.setName("file"); // Very important
		}
		return this.fileUpload;
	}

	private TextBox getTextBox() {
		if (this.textBox == null) {
			this.textBox = new TextBox();
			this.textBox.setName("userName");
		}
		return this.textBox;
	}
}
